package com.search.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5EncryptTool {
    public static String encry(String str){
        try {
            // 创建MD5 Hash
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 对字符串进行MD5加密
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字节
            byte[] bytes = md.digest();

            // 将byte转化为16进制表示的字符串
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.length; i++) {
                String hex = Integer.toHexString(0xff & bytes[i]);
                if (hex.length() == 1) {
                    sb.append('0');
                }
                sb.append(hex);
            }

            // 返回32位加密字符串
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
